package com.kimho.product.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.core.PageInfo;
import com.kimho.product.domain.Product;
import com.kimho.product.service.ProductService;
import com.kimho.user.domain.User;
import com.kimho.util.Util;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private ProductService productService;

	@RequestMapping(value = "/productlist", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> getAllProduct(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> responseMap = new HashMap<String, Object>();
		PageInfo pi = new PageInfo(request);
		pi.setQueryParam("query", request.getParameter("query"));
		pi.setQueryParam("flag", request.getParameter("flag"));
		if((User)request.getSession().getAttribute("user")!=null && ((User)request.getSession().getAttribute("user")).getPost_id()!=1){//如果登陆用户的较色不是1(经理),就只查询登陆地点的数据
			pi.setQueryParam("login_location", request.getSession().getAttribute("login_localtion"));
		}
		List<Product> products = this.productService.getAllProduct(pi);
		responseMap.put("rows", products);
		responseMap.put("totalCount", this.productService.getTotal(pi));
		return responseMap;
	}
	@RequestMapping(value = "/productlist_s", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> getAllProduct_s(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> responseMap = new HashMap<String, Object>();
		PageInfo pi = new PageInfo(request);
		pi.setQueryParam("query", request.getParameter("query"));
		pi.setQueryParam("flag", request.getParameter("flag"));
		if((User)request.getSession().getAttribute("user")!=null && ((User)request.getSession().getAttribute("user")).getPost_id()!=1){//如果登陆用户的较色不是1(经理),就只查询登陆地点的数据
			pi.setQueryParam("login_location", request.getSession().getAttribute("login_localtion"));
		}
		List<Product> products = this.productService.getAllProduct_s(pi);
		responseMap.put("rows", products);
		responseMap.put("totalCount", this.productService.getTotal_s(pi));
		return responseMap;
	}

	@RequestMapping(value = "/deletelogobj")
	public @ResponseBody Map<String, Object> deletelogobj(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> responseMap = new HashMap<String, Object>();

		try {
			String ids = request.getParameter("ids");
			String[] id = null;
			if (ids != null && !ids .equals("") ) {
				id = ids.split(",");
			}
			this.productService.deletelogobj(id);
			responseMap.put("success", "true");
			responseMap.put("info", "删除成功！");
			return responseMap;
		} catch (Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", "删除失败！  " + e.getClass() + ":"
					+ e.getMessage());
			return responseMap;
		}
	}

	@RequestMapping(value = "/savelogobj")
	public @ResponseBody Map<String, Object> savelogobj(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> responseMap = new HashMap<String, Object>();
		try {
			String ids = request.getParameter("ids");
			String logistics_id = request.getParameter("logistics_id");
			String[] object_ids = null;
			if (ids != null && ids != "") {
				object_ids = ids.split(",");
			}
			this.productService.savelogobj(object_ids, logistics_id);
			responseMap.put("success", "true");
			responseMap.put("info", "保存成功！");
			return responseMap;
		} catch (Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", "保存失败！  " + e.getClass() + ":"
					+ e.getMessage());
			return responseMap;
		}
	}

	@RequestMapping(value = "/deletelogobjbylogid")
	public @ResponseBody Map<String, Object> deletelogobjbylogid(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> responseMap = new HashMap<String, Object>();

		try {
			String ids = request.getParameter("ids");
			String[] id = null;
			if (ids != null && ids != "") {
				id = ids.split(",");
			}
			this.productService.deletelogobjbylogid(id);
			responseMap.put("success", "true");
			responseMap.put("info", "删除成功！");
			return responseMap;
		} catch (Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", "删除失败！  " + e.getClass() + ":"
					+ e.getMessage());
			return responseMap;
		}
	}
	
	@RequestMapping(value = "/save")
	public @ResponseBody Map<String, Object> save(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> responseMap = new HashMap<String, Object>();
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			String sign_time = request.getParameter("sign_date")+" "+request.getParameter("sign_time");
			String payout_time = request.getParameter("payout_date")+" "+request.getParameter("payout_time");
			param.put("logistics_id", request.getParameter("logistics_id"));
			param.put("deal_id", request.getParameter("deal_id"));
			param.put("local_start_id", (String) request.getSession().getAttribute("login_localtion"));//登陆地点获取
			param.put("local_end_id", request.getParameter("local_end_id"));
			param.put("truck_id", request.getParameter("truck_id"));
			param.put("sign_staff_id", request.getParameter("sign_staff_id"));
			param.put("payout_staff_id", request.getParameter("payout_staff_id"));
			param.put("trucker_staff_id", request.getParameter("trucker"));
			param.put("trucker_other_staff_id", request.getParameter("trucker_other"));
			param.put("state", "0");
			param.put("sign_time", sign_time);
			param.put("payout_time", payout_time);
			if(request.getParameter("logistics_id")==null || request.getParameter("logistics_id").trim().equals("")){
				param.put("logistics_id", Util.getDateID());
				this.productService.save(param);
				responseMap.put("success", "true");
				responseMap.put("info", "添加物流单成功！");
				return responseMap;
			}else{
				this.productService.update(param);
				responseMap.put("success", "true");
				responseMap.put("info", "编辑物流单成功！");
				return responseMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", "添加物流单失败！  " + e.getClass() + ":"
					+ e.getMessage());
			return responseMap;
		}
	}
	/**
	 * 根据post_id取得对应的员工
	 */
	@RequestMapping(value="/getTrucker")
	public @ResponseBody List<Map<String, String>> getTruckerByPost_id(HttpServletRequest request,
			HttpServletResponse response){
		return this.productService.getTruckerByPost_id(request.getParameter("post_id"));
	}
	
	/**
	 * 发车
	 */
	@RequestMapping(value="/startcar")
	public @ResponseBody Map<String, Object> startCar(HttpServletRequest request,
			HttpServletResponse response){
		Map<String, Object> responseMap = new HashMap<String, Object>();
		try {
			this.productService.startCar(request.getParameter("logistics_id"));
			responseMap.put("success", true);
			responseMap.put("info", "发车成功！");
			return responseMap;
		} catch (Exception e) {
			e.printStackTrace();
			responseMap.put("success", false);
			responseMap.put("info", "发车失败！");
			return responseMap;
		}
		
	}
}
